package com.oa.controller;

import com.oa.vo.Message;
import com.oa.vo.Page;
import com.oa.vo.Result;
import com.oa.pojo.Attendance;
import com.oa.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@CrossOrigin
@ResponseBody
@Controller
public class AttendanceConntroller {

    @Autowired
    private AttendanceService attendanceService;

    @RequestMapping(value = "/getAttendancePage")
    public Result getAttendancePage(Integer pageNo, Integer pageSize){
        if(pageNo == null){
            pageNo=1;
        }
        if(pageSize == null){
            pageSize=3;
        }
        Page page = attendanceService.getAttendancePage(pageNo,pageSize);
        Message message = new Message();
        Result result = new Result();
        result.setData(page);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/getAttendance")
    public Result getAttendance(Integer attendanceId){
        Attendance attendance = attendanceService.getAttendance(attendanceId);
        Message message = new Message();
        Result result = new Result();
        result.setData(attendance);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/addAttendance")
    public Result addAttendance(@RequestBody Attendance attendance){
        System.out.println(attendance);
        attendanceService.addAttendance(attendance);
        Result result = new Result();
        return result;
    }

    @RequestMapping(value = "/deleteAttendance")
    public Result deleteAttendance(Integer attendanceId){
        Message message = new Message();
        Result result = new Result();
        if(attendanceId == null){
            message.setCode(200);
            message.setMsg("No ID");
            result.setMessage(message);
            return result;
        }
        message.setCode(100);
        message.setMsg("ok");
        attendanceService.deleteAttendance(attendanceId);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/updateAttendance")
    public Result updateAttendance(@RequestBody Attendance attendance){
        attendanceService.updateAttendance(attendance);
        Result result = new Result();
        return result;
    }

}
